package com.itcast.demo.demo1.freemarker.controller;

import com.itcast.demo.demo1.freemarker.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @GetMapping("/test")
    public String hello(Model model) {
        // 存储数据到model
        model.addAttribute("name", "jack");

        Student student = new Student();
        student.setName("zhangsan");
        model.addAttribute("student", student);

        return "student";
    }

    @GetMapping("/list")
    public String list(Model model) {
        Student stu1 = new Student();
        stu1.setName("zhangsan");
        stu1.setAge(18);
        stu1.setBirthday(new Date());
        stu1.setMoney(100f);

        Student stu2 = new Student();
        stu2.setName("小明");
        stu2.setAge(28);
        stu2.setBirthday(new Date());
        stu2.setMoney(9800f);

        Student stu3 = new Student();
        stu3.setName("李四");
        stu3.setAge(9);
        stu3.setBirthday(new Date());
        stu3.setMoney(240f);

        List<Student> stus = Arrays.asList(stu1, stu2, stu3);

        model.addAttribute("stus", stus);
        return "list";
    }

    @GetMapping("/map")
    public String map(Model model) {

        Student stu1 = new Student();
        stu1.setName("zhangsan");
        stu1.setAge(18);
        stu1.setBirthday(new Date());
        stu1.setMoney(100f);

        Student stu2 = new Student();
        stu2.setName("小明");
        stu2.setAge(28);
        stu2.setBirthday(new Date());
        stu2.setMoney(9800f);

        Student stu3 = new Student();
        stu3.setName("李四");
        stu3.setAge(9);
        stu3.setBirthday(new Date());
        stu3.setMoney(240f);

        HashMap<String, Student> stuMap = new HashMap<>();
        stuMap.put("stu1", stu1);
        stuMap.put("stu2", stu2);
        stuMap.put("stu3", stu3);

        model.addAttribute("stuMap", stuMap);
        return "map";
    }
}
